//
//  HNSH_ChatFriendDetailViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/16.
//

#import "HNSH_ChatFriendDetailViewController.h"
#import "HNSH_ChatFriendModel.h"

//Vc
#import "HNSH_ChatSingleViewController.h"       //单聊
#import "HNSH_RemarkViewController.h"           //备注
#import "HNSH_ReportViewController.h"           //举报
#import "HNSH_AddFriendsViewController.h"       //添加好友

#import "HNSH_ChatFriendDetailItem.h"

@interface HNSH_ChatFriendDetailViewController ()

/** */
@property (nonatomic, strong)UIView * bgView;
/** */
@property (nonatomic, strong)UIImageView * imgView;
/** */
@property (nonatomic, strong)UILabel * nameLabel;
/** */
@property (nonatomic, strong)UILabel * levelLabel;
/** */
@property (nonatomic, strong)UILabel * phoneLabel;
/** */
@property (nonatomic, strong)UILabel * idLabel;

/* 备注 */
@property (nonatomic, strong) HNSH_ChatFriendDetailItem *remarkItem;
/* 删除 */
@property (nonatomic, strong) UIButton *deleteItem;

/* addBtn */
@property (nonatomic, strong) UIButton *addBtn;
/** */
@property (nonatomic, strong)UIButton * sendBtn;
/** */
@property (nonatomic, strong)HNSH_ChatFriendUserModel * userModel;

@end

@implementation HNSH_ChatFriendDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadUserInfo];
}

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"好友信息" showLeftItem:YES];

    self.view.backgroundColor = [UIColor appBGColor];
    
    [self addAllViews];
    
    [self addAllLayout];
}

- (void)addAllViews{
    
    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.imgView];
    [self.bgView addSubview:self.nameLabel];
    [self.bgView addSubview:self.levelLabel];
    [self.bgView addSubview:self.phoneLabel];
    [self.bgView addSubview:self.idLabel];
    
    [self.view addSubview:self.remarkItem];
    [self.view addSubview:self.deleteItem];
    
    [self.view addSubview:self.sendBtn];
    [self.view addSubview:self.addBtn];
}

- (void)addAllLayout{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(kNavBarAndStatusBarHeight);
        make.height.mas_equalTo(216);
    }];
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.width.height.mas_equalTo(72);
        make.top.mas_equalTo(24);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imgView.mas_bottom).offset(16);
        make.centerX.mas_equalTo(-21);
        make.height.mas_equalTo(28);
    }];
    [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(8);
        make.centerY.equalTo(self.nameLabel);
        make.width.mas_equalTo(26);
        make.height.mas_equalTo(17);
    }];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(8);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
    [self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneLabel.mas_bottom).offset(4);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
    
    [self.remarkItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgView.mas_bottom).offset(8);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(-0);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.remarkItem.mas_bottom).offset(8);
    }];
    
    [self.deleteItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.sendBtn.mas_bottom).offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(-0);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.remarkItem.mas_bottom).offset(8);
    }];
}

- (void)loadUserInfo{
    [HNSH_ChatNetServer requestRongCloudFriendDetailWithRcUserId:self.rcUserId success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            HNSH_ChatFriendUserModel * model = [HNSH_ChatFriendUserModel yy_modelWithDictionary:responseObject.resultset];
            self.userModel = model;
            
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Custom Methods
- (void)remarkGesAction{
    HNSH_RemarkViewController * vc = [HNSH_RemarkViewController new];
    vc.rcUserId = self.userModel.user_id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)reportGesAction{
    HNSH_ReportViewController * vc = [HNSH_ReportViewController new];
    vc.rcUserId = self.rcUserId;
    vc.name = self.userModel.display_name.length ? self.userModel.display_name : self.userModel.name;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)sendBtnAction{
    HNSH_ChatSingleViewController * vc = [HNSH_ChatSingleViewController new];
    vc.conversationType = ConversationType_PRIVATE;
    vc.targetId = self.rcUserId;
    vc.title = self.userModel.display_name.length ? self.userModel.display_name : self.userModel.name;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)deleteBtnAction{
    WeakSelf(weakSelf);
    [HNSH_AlertView showTitle:@"确定要删除该好友吗" des:@"" buttonTitle:@"删除" buttonColor:[UIColor appGreenColor] buttonClick:^{
        [weakSelf deleteFriend];
    }];
}

- (void)deleteFriend{
    [HNSH_ChatNetServer requestRongCloudFriendshipDeleteWithRcUserId:self.userModel.user_id success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"删除成功"];
            //删除消息
            [[RCIMClient sharedRCIMClient] clearMessages:ConversationType_PRIVATE targetId:self.rcUserId];
            //删除本地会话
            [[RCIMClient sharedRCIMClient] removeConversation:ConversationType_PRIVATE targetId:self.rcUserId];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)addBtnAction{
    HNSH_AddFriendsViewController * vc = [HNSH_AddFriendsViewController new];
    vc.rcUserId = self.userModel.user_id;
    vc.name = self.userModel.name;
    [self.navigationController pushViewController:vc animated:YES];
}

//MARK: Setter Methods
- (void)setUserModel:(HNSH_ChatFriendUserModel *)userModel{
    _userModel = userModel;
    
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:userModel.portrait] placeholderImage:[UIImage imageNamed:@"base_user_header_icon"]];
    NSDictionary *dict = [userModel.extra jsonValueDecoded];
    self.levelLabel.text = [NSString stringWithFormat:@"V%ld",[dict[@"level"] integerValue]];
    
    self.nameLabel.text = userModel.display_name.length ? userModel.display_name : userModel.name;
    NSDictionary * extra = [userModel.extra jsonValueDecoded];
    self.phoneLabel.text = [NSString stringWithFormat:@"手机号：%@",[NSString transPhoneWithMobile:extra[@"phone"]]];
    
    self.idLabel.text = [NSString stringWithFormat:@"ID：%@",userModel.user_id];
    if (userModel.friend_status) {
        self.addBtn.hidden = YES;
        self.sendBtn.hidden = NO;
        self.deleteItem.hidden = NO;
    } else {
        self.addBtn.hidden = NO;
        self.sendBtn.hidden = YES;
        self.deleteItem.hidden = YES;
    }
}

//MARK: Getter Methods
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
    }
    return _bgView;
}

- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.layer.cornerRadius = 8;
        _imgView.layer.masksToBounds = YES;
        _imgView.image = [UIImage imageNamed:@"base_user_header_icon"];
    }
    return _imgView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont boldSystemFontOfSize:20];
        _nameLabel.textColor = [UIColor appBlack333LabelColor];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

- (UILabel *)levelLabel{
    if (!_levelLabel) {
        _levelLabel = [UILabel new];
        _levelLabel.font = [UIFont systemFontOfSize:12];
        _levelLabel.textColor = [UIColor appWhiteLabelColor];
        _levelLabel.textAlignment = NSTextAlignmentCenter;
        _levelLabel.backgroundColor = [UIColor colorWithHexString:@"#FB6F1C"];
        _levelLabel.layer.cornerRadius = 4.0f;
        _levelLabel.layer.masksToBounds = YES;
    }
    return _levelLabel;
}

- (UILabel *)phoneLabel{
    if (!_phoneLabel) {
        _phoneLabel = [UILabel new];
        _phoneLabel.font = [UIFont systemFontOfSize:14];
        _phoneLabel.textColor = [UIColor appGrayLabelColor];
        _phoneLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _phoneLabel;
}

- (UILabel *)idLabel {
    if (!_idLabel) {
        _idLabel = [UILabel new];
        _idLabel.font = [UIFont systemFontOfSize:14];
        _idLabel.textColor = [UIColor appGrayLabelColor];
        _idLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _idLabel;
}

- (HNSH_ChatFriendDetailItem *)remarkItem {
    if (!_remarkItem) {
        _remarkItem = [[HNSH_ChatFriendDetailItem alloc]init];
        _remarkItem.titleLabel.text = @"备注";
        WeakSelf(weakSelf);
        _remarkItem.clickBlock = ^{
            [weakSelf remarkGesAction];
        };
    }
    return _remarkItem;
}

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton new];
        [_addBtn setImage:[UIImage imageNamed:@"chat_user_detal_icon_tong"] forState:UIControlStateNormal];
        [_addBtn setTitle:@"添加到通讯录" forState:UIControlStateNormal];
        [_addBtn setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _addBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _addBtn.backgroundColor = [UIColor appWhiteLabelColor];
        [_addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _addBtn.hidden = YES;
        [_addBtn HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextRight imageTitleSpace:8];
    }
    return _addBtn;
}

- (UIButton *)sendBtn{
    if (!_sendBtn) {
        _sendBtn = [UIButton new];
        [_sendBtn setImage:[UIImage imageNamed:@"chat_detail_send_msg_icon"] forState:UIControlStateNormal];
        [_sendBtn setTitle:@"发消息" forState:UIControlStateNormal];
        [_sendBtn setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _sendBtn.backgroundColor = [UIColor appWhiteLabelColor];
        [_sendBtn addTarget:self action:@selector(sendBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn.hidden = YES;
        [_sendBtn HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextRight imageTitleSpace:8];
    }
    return _sendBtn;
}

- (UIButton *)deleteItem{
    if (!_deleteItem) {
        _deleteItem = [UIButton new];
        [_deleteItem setImage:[UIImage imageNamed:@"chat_detail_delete_icon"] forState:UIControlStateNormal];
        [_deleteItem setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteItem setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _deleteItem.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _deleteItem.backgroundColor = [UIColor appWhiteLabelColor];
        [_deleteItem addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _deleteItem.hidden = YES;
        [_deleteItem HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextRight imageTitleSpace:8];
    }
    return _deleteItem;
}

@end
